package com.jyz.support.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jyz.support.exceptions.DAOException;


public class HbmDaoSupport extends HibernateDaoSupport {
	private final static Logger logger = Logger.getLogger(HbmDaoSupport.class.getName());

	public Object create(Object obj) throws DAOException {
		try {
			logger.debug("DAO create => " + obj);
			return getHibernateTemplate().save(obj);
		} catch (Exception e) {
			logger.error("DAO create => " + e.getMessage());
			throw new DAOException(e);
		}
	}

	public void update(Object obj) throws DAOException {
		try {
			logger.debug("DAO update => " + obj);
			getHibernateTemplate().update(obj);
		} catch (Exception e) {
			logger.error("DAO update => " + e.getMessage());
			throw new DAOException(e);
		}
	}

	public void delete(Object obj) throws DAOException {
		try {
			logger.debug("DAO delete => " + obj);
			getHibernateTemplate().delete(obj);
		} catch (Exception e) {
			logger.error("DAO delete => " + e.getMessage());
			throw new DAOException(e);
		}
	}
	
	public void deleteAll(List obj) throws DAOException {
		try {
			logger.debug("DAO deleteAll => " + obj);
			getHibernateTemplate().deleteAll(obj);
		} catch (Exception e) {
			logger.error("DAO deleteAll => " + e.getMessage());
			throw new DAOException(e);
		}
	}

	public List findAll(Class clazz) throws DAOException {
		try {
			logger.debug("DAO findAll => " + clazz.getName());
			return getHibernateTemplate().loadAll(clazz);
		} catch (Exception e) {
			logger.error("DAO findAll => " + e.getMessage());
			throw new DAOException(e);
		}
	}

	public Object findByID(Class clazz, Serializable id) throws DAOException {
		try {
			logger.debug("DAO findByID => " + clazz.getName() + " pk:" + id);
			Object obj = getHibernateTemplate().load(clazz, id);
			return obj;
		} catch (org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException se) {
			logger.error("DAO findByID => " + se.getMessage());
			return null;
		} catch (Exception e) {
			logger.error("DAO findByID => " + e.getMessage());
			throw new DAOException(e);
		}
	}

	public List findByHQL(String hql) throws DAOException {
		try {
			logger.debug("DAO findByHQL => " + hql);
			return getHibernateTemplate().find(hql);
		} catch (Exception e) {
			logger.error("DAO findByHQL => " + e.getMessage());
			throw new DAOException(e);
		}
	}
	
	public List findByCriteria(DetachedCriteria criteria) throws DAOException {
		try {
			logger.debug("DAO findByCriteria => " + criteria);
			return getHibernateTemplate().findByCriteria(criteria);
		} catch (Exception e) {
			logger.error("DAO findByCriteria => " + e.getMessage());
			throw new DAOException(e);
		}
	}
	
	public List findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults) throws DAOException {
		try {
			logger.debug("DAO findByCriteria => " + criteria + "|" + firstResult + "|" + maxResults);
			return getHibernateTemplate().findByCriteria(criteria, firstResult, maxResults);
		} catch (Exception e) {
			logger.error("DAO findByCriteria => " + e.getMessage());
			throw new DAOException(e);
		}
	}
	
    public List pagedList(String hql, int recordIndex, int pageSize) throws DAOException {
    	try {
    		logger.debug("DAO pagedList => " + hql + "|" + recordIndex + "|" + pageSize);
			Query query = getSession().createQuery(hql);
			return query.setFirstResult(recordIndex).setMaxResults(pageSize).list();
		} catch (Exception e) {
			logger.error("DAO pagedList => " + e.getMessage());
			throw new DAOException(e);
		}
    } 
	
//	public void synchronizedAction(LogDownloadDetail log) throws DAOException {
//	}
}
